//package com.jackrain.clouddata.oms.mq;
//
//import com.alibaba.fastjson.JSONArray;
//import com.alibaba.fastjson.JSONObject;
//import com.aliyun.openservices.ons.api.Action;
//import com.aliyun.openservices.ons.api.ConsumeContext;
//import com.aliyun.openservices.ons.api.Message;
//import com.aliyun.openservices.ons.api.MessageListener;
//import com.aliyun.openservices.ons.api.impl.util.MsgConvertUtil;
//import com.jackrain.nea.config.PropertiesConf;
//import com.jackrain.nea.core.db.Tools;
//import com.jackrain.nea.util.ApplicationContextHandle;
//import com.jackrain.nea.web.query.QueryEngine;
//import lombok.extern.slf4j.Slf4j;
//
//import java.io.Serializable;
//import java.sql.SQLException;
//import java.sql.Timestamp;
//
///**
// * 消费云数据消息
// */
//
//@Slf4j
//public class MQMessageListener implements MessageListener {
//
//    public MQMessageListener() {
//
//    }
//
//    @Override
//    public Action consume(Message message, ConsumeContext consumeContext) {
//
////        log.info("=======================执行consume====================");
////        ConsumeDao consumeDao = ApplicationContextHandle.getBean(ConsumeDao.class);
//
//        try {
//            Serializable ser = null;
//            ser = MsgConvertUtil.objectDeserialize(message.getBody());
//
//            log.debug("mqConsume ser:" + ser.toString());
//
//            if (ser instanceof JSONArray) {
//                JSONArray paramArray = JSONArray.parseArray(ser.toString());
//                log.debug("paramArray:" + paramArray.toJSONString());
//
//                //数据库操作 By BigZhi
//
//            }else if(ser instanceof JSONObject){
//                JSONObject paramObject = JSONObject.parseObject(ser.toString());
//                log.debug("paramObject:" + paramObject.toJSONString());
//
//                //数据库操作 By BigZhi
//                if( heartBeatOp(paramObject) > 0 ){
//                    log.info("====================数据更新成功！======================");
//                }else{
//                    log.info("====================数据更新失败！======================");
//                }
//            }
//
//            PropertiesConf pconf = ApplicationContextHandle.getBean(PropertiesConf.class);
//
//            String topic = pconf.getProperty("ons.mq.topic1", "");
//            if (message.getTopic().equals(topic)) {
//                log.info("msgBody:" + message.getBody().toString());
//            } else {
//                log.error("not match ons.mq.topic1");
//            }
//
//        } catch (Exception e) {
//
//            e.printStackTrace();
//
//            // 出现异常,再次消费
//            return Action.ReconsumeLater;
//        }
//
//        // 消费成功,更新系统日志表
//        return Action.CommitMessage;
//    }
//
//    //OMS_HEARTBEAT数据库操作
//    public int heartBeatOp(JSONObject paramObject) throws SQLException {
//        log.info("==================调用dao=================");
//
//        String ip = paramObject.getString("ip");
//        String no = paramObject.getString("version");
//        String env = paramObject.getString("environment");
//        Timestamp handletime = paramObject.getTimestamp("handletime");  //最新HANFDLETIME
//
//        Timestamp last_handletime; //上一次HANDLETIME
//        String distantime;  //上次报文间隔（秒）
//
//        int code;
//        String sql;
//
//        //获取明细最近一次HANDLETIME
//        sql = "select * from OMS_MANTIS where NO=? order by HANDLETIME DESC limit 1";
//        JSONObject jo = QueryEngine.getInstance().doQueryObject(sql,new Object[]{no});
//
//        Timestamp nowdate = new Timestamp(System.currentTimeMillis());  //创建和修改时间
//
//        //添加明细数据
//        log.info("================添加明细数据=================");
//        Long OMS_MANTIS_id = Tools.getSequence("OMS_MANTIS");
//        sql = "insert into OMS_MANTIS (ID, AD_CLIENT_ID, IP, NO, ENV, HANDLETIME, OWNERID, MODIFIERID, CREATIONDATE, MODIFIEDDATE) values(?,?,?,?,?,?,?,?,?,?)";
//        code = QueryEngine.getInstance().executeUpdate(sql, new Object[]{OMS_MANTIS_id, 37, ip, no, env, handletime, 893, 893, nowdate, nowdate});
//
//        if(code <= 0){ //添加失败
//            log.info("===============添加明细数据失败==============");
//            return code;
//        }
//
//        //测试监控数据是否添加成功
//        //sql="select * from OMS_MANTISREPORT";
//        //JSONArray reports = QueryEngine.getInstance().doQueryObjectArray(sql);
//        //System.out.println(reports.getJSONObject(1).getString("NO"));
//
//        //检查是否含有某NO的监控数据
//        sql="select * from OMS_MANTISREPORT where NO=? limit 1";
//        JSONObject report = QueryEngine.getInstance().doQueryObject(sql,new Object[]{no});
//
//        //更新监控数据
//        if(jo != null){
//            last_handletime = jo.getTimestamp("HANDLETIME");
//            distantime = ( handletime.getTime() - last_handletime.getTime() )/1000 + "";
//
//            if(report != null){
//                log.info("===================更新监控数据====================");
//
//                sql = "update OMS_MANTISREPORT set IP=?, DISTANTIME=?, MODIFIEDDATE=? where NO=?";
//                code = QueryEngine.getInstance().executeUpdate(sql, new Object[]{ip, distantime, nowdate, no});
//
//            }else{
//                log.info("===================添加监控数据(distantime有值)====================");
//
//                Long OMS_MANTISREPORT_id = Tools.getSequence("OMS_MANTISREPORT");
//
//                sql = "insert into OMS_MANTISREPORT (ID, AD_CLIENT_ID, IP, NO, DISTANTIME, OWNERID, MODIFIERID, CREATIONDATE, MODIFIEDDATE) values(?,?,?,?,?,?,?,?,?)";
//                code = QueryEngine.getInstance().executeUpdate(sql, new Object[]{OMS_MANTISREPORT_id, 37, ip, no, distantime, 893, 893, nowdate, nowdate});
//            }
//
//        }else{
//            log.info("=================添加监控数据(distantime无值)=================");
//
//            Long OMS_MANTISREPORT_id = Tools.getSequence("OMS_MANTISREPORT");
//
//            sql = "insert into OMS_MANTISREPORT (ID, AD_CLIENT_ID, IP, NO, DISTANTIME, OWNERID, MODIFIERID, CREATIONDATE, MODIFIEDDATE) values(?,?,?,?,?,?,?,?,?)";
//            code = QueryEngine.getInstance().executeUpdate(sql, new Object[]{OMS_MANTISREPORT_id, 37, ip, no, 0, 893, 893, nowdate, nowdate});
//        }
//
//        return code;
//    }
//}